/*
 * Graph.cpp
 *
 *  Created on: 01-09-2013
 *      Author: Krzychu
 */

#include "Graph.h"

#include <stdio.h>
#include <string.h>

#define UNDEFINED -1 // the value which graph targets are initialised with

Graph::Graph(int vertexNumber, int edgeNumber)
{
	this->edgeNumber = edgeNumber;
	this->vertexNumber = vertexNumber;
	this->vertexCounter = 1;
	this->initial = 0;
	this->target = new int[edgeNumber];

	for (int i = 0; i < edgeNumber; i++)
	{
		this->target[i] = UNDEFINED;
	}
}

Graph::~Graph()
{
	delete this;
}

int Graph::newVertex()
{
	if (vertexCounter <= vertexNumber)
	{
		return vertexCounter++;
	}
	else
	{
		return -1;
	}
}

int Graph::getInitial()
{
	return initial;
}

void Graph::setTarget(int source, unsigned char label, int target)
{
	if (source < vertexNumber && source * label <= edgeNumber
			&& target < vertexNumber)
	{
		this->target[source * (edgeNumber / vertexNumber) + label] = target;
	}
}

int Graph::getTarget(int source, unsigned char label)
{
	int result = -1;

	if (source < vertexNumber && source * label < edgeNumber)
	{
		result = this->target[source * (edgeNumber / vertexNumber) + label];
	}

	return result;
}

void Graph::copyVertex(int target, int source)
{
	if (target < vertexNumber && source < vertexNumber)
	{
		memcpy(this->target + target * (edgeNumber / vertexNumber),
				this->target + source * (edgeNumber / vertexNumber),
				(edgeNumber / vertexNumber) * sizeof(int));
	}
}

int Graph::getUndefined()
{
	return UNDEFINED;
}
